/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author luan
 */
import java.io.*;
import java.util.Scanner;
public class Bank {
    static int lastAccountNumber=0; // the index of the last account
    static String fileName = "Accounts.bin";// Name of file that stores the accounts
    Account[] accounts; // array stores accounts.
    Scanner sc=new Scanner(System.in);
    /** Creates a new instance of Bank */
    //--------------------------------------
    // create 100 accounts in accounts array
    // open file Accounts.bin
    // read accounts from Accounts.bin into the  accounts array by create object of ObjectInputStream
    //----------------------------------------
    public Bank()
    {
        accounts= new Account[100]; //Create array with 100 items
        File f=new File(fileName);
        if(!f.exists()) return; // If file not exists out program
        try
        {
            FileInputStream fs=new FileInputStream(f); // create FileInputStream fs
            ObjectInputStream os=new ObjectInputStream(fs);// Create ObjectInputStream os
            Account acc=null; // the variable stores the account from file Accounts.bin
            while((acc=(Account)os.readObject())!=null) // loop until acc= null that means until no more account in file
            {
                accounts[lastAccountNumber]=acc; //put into accounts array;
                lastAccountNumber++;
            }
            os.close();
            fs.close();
        }
        catch(Exception ex)
        {

        }
        System.out.println("Total account(s) were created "+lastAccountNumber);
    }
    //------------------------------------------------------------
    //------------------------createAccount-----------------------
    //receive a new accounts and insert it into accounts array
    //write the accounts array into file
    //---------------------------------------------------------------
    public void createAccount(Account objAcc) throws Exception
    {
        if(lastAccountNumber>100)
            throw new Exception("the number of account was full");
        accounts[lastAccountNumber]=objAcc; //insert one account into the last of array
        lastAccountNumber++;
        System.out.println("Account was created sucessfully");
        //write an account into file============================//
        try
        {
            FileOutputStream fo=new FileOutputStream(fileName);
            ObjectOutputStream os=new ObjectOutputStream(fo);
            for(int i=0;i<lastAccountNumber;i++)
            {
                os.writeObject(accounts[i]);
            }
            os.close();
            fo.close();
        }
        catch(Exception ex)
        {

        }
    }
    //-------------------------------------------------
    //---------------find-----------------------------
    //find a particular account by account number
    // return account object if found. Else return null
    //---------------------------------------------------
    public Account find(int no)
    {
        for(int i=0;i<lastAccountNumber;i++)
        {
            if(accounts[i].getAccountNumber()==no)
                return accounts[i];
        }
        return null;
    }
    //----------====================================
    //----------------display------------------------
    //input a particular account no, if exist display
    // on the screen else print out : throw exception
    //==============================================
    public void display() throws Exception
    {
        int no=-1;
        try{
            System.out.println("Input Account no that you want to show: ");
            no=sc.nextInt();
        }
            catch(Exception ex)
        {
            System.out.println("Account number must be a numberic.....");
            System.exit(1);
            
        }
           Account acc=find(no);
           if(acc==null)
           {
                throw new Exception("Account not found");
            }
           else
               System.out.println(acc);
        
        
    }
    //=========================================================================
    //=============================withdraw====================================
    //input account number
    //if not exist throw exception " account not exist"
    //else input the amount
    // if amount <0  : throw exception "can not less than 0"
    // if amount > balance: throw exception: " can not more than balance"
    //==========================================================================
    public void withdraw() throws Exception
    {   int accNo=-1;
        //input account number
                try{
                System.out.println("Input the account number that you want to withdraw: ");
                accNo=sc.nextInt();
                }
                catch(Exception ex)
                {
                    System.out.println("Wrong key in must be numberic.....");
                    System.exit(1);
                }
        //find the account
        Account acc=find(accNo);
        //if null throw exception
        if(acc==null)
            throw new Exception("This account is not exist....");
        else
        {   //============
            //input amount
            //============
            double amount=0;
            try{
            
            System.out.println("Input the amount that you want to withdraw");
            amount=sc.nextDouble();
            }
            catch(Exception ex)
            {
                System.out.println("Wrong key in must be numberic....");
                System.exit(1);
            }
            //==========================
            //if amount <0 throw exception
            //===========================
            if(amount<0)
            {
                throw new Exception("Amount can not <0");
            }
            //===================================
            //if amount > balance - 500 throw exception
            //===================================
            else if(amount>(acc.getAccountBalance()-500))
            {
                throw new Exception("Amount must the <+Balance-500");
            }
            //===========================
            //withdraw by call withdraw menthod of class Bank
            //=============================
            else
            {
                acc.withdraw(amount);
                System.out.println("Withdraw sucessful");
            }
        }
       

    }
    //============================================================
    //====================deposit=================================
    //Input account number
    //if account number not exist throw exception
    //else input the amount want to deposit
    //if the amount< 0 throw exception
    //else deposit and print out sucessfull
    //==============================================================
    public void deposit() throws Exception
    {
        int accNo=-1;
        double amount=0;
            //===============
            //input and find account by account number
            //================
            try{
            System.out.println("Enter the Account number that you want to deposit: ");
            accNo=sc.nextInt();
            }
            catch(Exception ex)
            {
                System.out.println("Wrong key in must be numberic....");
                System.exit(1);
            }
            Account acc=find(accNo);
            //=================
            //if not found throw exception
            //=================
            if(acc==null)
                throw new Exception("Account does not exist");
            //===================
            //if found input the amount
            //===================
            else
            {   try{
                System.out.println("Input the amount that you want to deposit: ");
                amount=sc.nextDouble();
                }
                catch(Exception ex)
                 {
                    System.out.println("Wrong key in must be numberic....");
                    System.exit(1);
                 }
                //=============
                //if amount<0 throw exception
                //===============
                if(amount<0)
                {
                    throw new Exception("Amount can not less than 0");
                }
                //==============
                //deposit using the deposit menthod of class Bank
                //=============
                else
                {
                    acc.deposit(amount);
                    System.out.println("Deposit sucessfully");
                }
            }
    }
}
